package com.Offer;

import com.pojo.ListNode;

import java.util.List;

/*
    面试题18：删除链表的节点
    给定单向链表的头指针和一个节点指针，在 O(1)时间复杂度内删除该 节点。
    测试用例：1,2,3,4,5,6
 */
public class demo18 {

    public static void main(String[] args) {
        int[] input = new int[]{1,2,3,4,5,6};
        ListNode head = new ListNode(1);
        for(int i = 1;i<input.length;i++){
            head.add(input[i]);
        }
        ListNode deListNode = head.next.next;   //删除节点值为3的节点
        deleteNode(head,deListNode);
        System.out.println();
        for(int i = 0;i<input.length-1;i++){
            System.out.print(head.val+" ");
            head = head.next;
        }


    }

    public static void deleteNode(ListNode head,ListNode deListNode){
        if(deListNode == null || head == null){
            return;
        }

        if(deListNode == head){
            head = null;
        }else{
            if(deListNode.next == null){
                ListNode point = head;
                while(point.next.next != null){
                    point = point.next;
                }
                point.next = null;
            }else{
                deListNode.val = deListNode.next.val;
                deListNode.next = deListNode.next.next;
            }
        }
    }
}
